angular.module('mamApp').controller('planManageController', ['$scope', 'PlayControlManager', '$state', 'InitManger', '$filter','ImsErr',
    function ($scope, PlayControlManager, $state, InitManger, $filter, ImsErr) {
        var planId = $state.params.planId;
        $scope.channlErrShow=false;
        $scope.share=true;
        $scope.area=0;
        $scope.formData = {shareControl:1,downloadControl:1};
        $scope.provinces = JSON.parse(localStorage.getItem("provinces"));


        $scope.provinces.forEach(function (province) {
            province.cities = InitManger.getCitiesByProId(province.provinceID);
        })


        function initCity(citySelected) {

            $scope.provinces.forEach(function (province) {
                province.cities.forEach(function (item) {
                    if (citySelected.indexOf(item.cityID.toString()) > -1) {
                        item.checked = true;
                    }
                })
                if ($filter("filter")(province.cities, {checked: true}).length == province.cities.length) {
                    province.checked = true;
                }


            })


        }


        //详情并绑定数值
        var initPlanDetail = function (channels) {
            PlayControlManager.albumPlanDetail(planId).then(function (result) {
                if (result.data.c == 's') {
                    var playPlanVO = result.data.playPlanVO;
                    $scope.formData.name = playPlanVO.name;
                    $scope.formData.downloadControl = playPlanVO.downloadControl;
                    $scope.formData.shareControl = playPlanVO.shareControl;
                    var playPlans = playPlanVO.playPlan.split(",");
                    var citySelected = playPlanVO.localControl?playPlanVO.localControl.split(","):[];
                    if(citySelected.length){
                        $scope.area=1;
                        initCity(citySelected)

                    }else{
                        $scope.area=0;

                    }
                    channels.forEach(function (channel) {
                        channel.channelList.forEach(function (item) {
                            if (playPlans.indexOf(item.id.toString()) > -1) {
                                item.checked = true;
                                $scope.itemCheckChange(channel)
                            }
                        })
                    })


                }
            })
        }

        PlayControlManager.albumChannels().then(function (result) {
            if (result.data.c == "s") {
                $scope.channels = result.data.pageList;
                if (planId) {
                    initPlanDetail($scope.channels);
                }

            }


        })


        $scope.channelCheckedChange = function (channel) {
            channel.channelList.forEach(function (item) {
                item.checked = channel.checked;
            })
        }


        $scope.itemCheckChange = function (channel) {
            var itemChecked = [];
            channel.channelList.forEach(function (item) {
                if (item.checked) {
                    itemChecked.push(item.id);
                }

            })
            if (itemChecked.length == channel.channelList.length) {
                channel.checked = true;
            } else {
                channel.checked = false;

            }
        }
        $scope.submit = function () {
            var playPlan = [];
            $scope.channels.forEach(function (channel) {
                channel.channelList.forEach(function (item) {
                    if (item.checked) {
                        playPlan.push(item.id);
                    }
                })
            });

            if(!playPlan.length){
                $scope.channleErrShow=true;
                return false;
            }else{
                $scope.channleErrShow=false;
            }
            $scope.formData.playPlan = playPlan.join(",");
            $scope.formData.localControl = getSelectCityIds().join(",");
            if (planId) {
                $scope.formData.id = planId;
                PlayControlManager.albumPlanEdit($scope.formData).then(function (result) {
                    if (result.data.c == "s") {
                        $state.go("main.playPlan", {}, {reload: true})
                    } else {
                        ImsErr.open({message:"方案名称已存在"})
                    }
                })
            } else {
                PlayControlManager.albumPlanAdd($scope.formData).then(function (result) {
                    if (result.data.c == "s") {
                        $state.go("main.playPlan", {}, {reload: true})
                    } else {
                        ImsErr.open({message:"方案名称已存在"})
                    }
                })
            }

        }

        function getSelectCityIds() {
            var ids = []
            $scope.provinces.forEach(function (province) {
                province.cities.forEach(function (city) {
                    if (city.checked) {
                        ids.push(city.cityID);

                    }
                })


            })
            return ids

        }


        $scope.provinceCheckedChange = function (province) {
            province.cities.forEach(function (city) {
                city.checked = province.checked;
            })
        }

        $scope.cityCheckedChange = function (province, city) {
            province.checked = true;
            province.cities.forEach(function (city) {
                if (!city.checked) {
                    province.checked = false;
                }
            })
        }


    }])

